package cn.edu.gdpt.xxgc.mobile0123456789hwh0328;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

//1、初始化控件
//2、设置按钮监听器
//3、写处理单元
public class MainActivity extends AppCompatActivity {

    private EditText edtTxtMainNuma;
    private EditText edtTxtMainNumb;
    private Button btnMainResult;
    private TextView tvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                拿数据 处理数据 做加法  显示
                String numa = edtTxtMainNuma.getText().toString();
                String numb = edtTxtMainNumb.getText().toString();
                try {
                    int a = Integer.valueOf(numa);
                    int b = Integer.valueOf(numb);

                    int result = a + b;

                    tvMainResult.setText(String.valueOf(result));
                }catch (Exception e){
                    tvMainResult.setText("计算出错");
                }

            }
        });
    }

    private void initView() {
        edtTxtMainNuma = (EditText) findViewById(R.id.edtTxtMainNuma);
        edtTxtMainNumb = (EditText) findViewById(R.id.edtTxtMainNumb);
        btnMainResult = (Button) findViewById(R.id.btnMainResult);
        tvMainResult = (TextView) findViewById(R.id.tvMainResult);
    }
}